#!/bin/bash
#
# Checks the syntax of all the bash scripts in the Pulley distribution
#
# Author:   Dr. Mike Murphy (mmurphy2@coastal.edu)
# Revision: 11 November 2013
#
#   Copyright 2013 Coastal Carolina University
#
#   Licensed under the Apache License, Version 2.0 (the "License");
#   you may not use this file except in compliance with the License.
#   You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.
#

if [[ -z "${PULLEY_ROOT}" ]]; then
	myself=$(readlink -f "$0")
	whereami=$(dirname "${myself}")
	
	# Pulley root should be 2 directories up
	PULLEY_ROOT=$(dirname "${whereami}")
	PULLEY_ROOT=$(dirname "${PULLEY_ROOT}")
fi

OLDIFS="${IFS}"
IFS='
'

status=0

for entry in $(find "${PULLEY_ROOT}"); do
	file "${entry}" | grep Bourne-Again > /dev/null 2>&1
	if [[ $? -eq 0 ]]; then
		bash -n "${entry}"
		chk=$?
		[ ${status} -eq 0 ] && status=${chk}
		
		echo -n "Syntax ${entry}: "
		if [[ ${chk} -eq 0 ]]; then
			echo "OK"
		else
			echo "FAIL"
		fi
	fi
done

IFS="${OLDIFS}"

exit ${status}
